package handler

import (
	_ "net/http/pprof"

	"gitee.com/crack007/proxy/src/logserver/service"

	"gitee.com/crack007/goose"
	"gitee.com/crack007/goose/gscore/web"
	"gitee.com/crack007/proxy/src/logserver/handler"

	"gitee.com/crack007/goose/gscore"

	"gitee.com/crack007/goose/gscore/server"
	"gitee.com/crack007/proxy/src/config"
	"gitee.com/crack007/proxy/src/proxy/protocol/logdump"
)

type serverHandler struct {
	server.DefaultServerHandle
}

func NewServerHandler() *serverHandler {
	return &serverHandler{}
}

func (h *serverHandler) OnInitConfig() {
	logdump.Init(config.MysqlConfig.SplitInterval, config.MysqlConfig.SplitTag, config.MysqlConfig.SplitCount)
}

func (h *serverHandler) OnBeforeRun() {
	service.LogService.Init(config.LogServerConfig.Buffer)

	if h.GetServer().Builder.PprofPort > 0 {
		gscore.BootstrapPprof(h.GetServer().Builder.PprofPort)
	}
}

func (h *serverHandler) OnStartSlaveServer() {
	if config.LogServerConfig.Port > 0 {
		httpServerBuilder := web.NewHttpServerBuilder(&handler.LogServerHandler)
		httpServerBuilder.SetPort(config.LogServerConfig.Port)
		httpServerBuilder.SetServerName("LogServer")
		goose.ExecuteSlave(httpServerBuilder)
	}
}
